package cdu.notFound.controller;

import cdu.notFound.model.Goods;
import cdu.notFound.model.User;
import cdu.notFound.service.GoodsService;
import cdu.notFound.service.impl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * 发布商品
 */
@MultipartConfig(maxFileSize = 1024 * 1024 * 10)
@WebServlet("/GoodsCheckServlet")
public class GoodsCheckServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String nameCheck = "";
        String quantityCheck = "";
        String contentCheck = "";
        String fileCheck = "";
        response.setContentType("text/html;charset=UTF-8");
        request.setAttribute("isCheck", false);
        // 存储路径
        String savePath = request.getServletContext().getRealPath("static/goods_img");
        // 处理表单
        String goodsName = request.getParameter("name-goods");
        String goodsContent = request.getParameter("content-goods");
        String goodsQuantitystr = request.getParameter("quantity-goods");// 先判断再转换成int防止直接抛出异常
        // 处理文件
        Part part = request.getPart("file");
        String type_id =  request.getParameter("type_id-goods");
        String[] typeId = {"book", "live", "clothes", "eletronic", "sport", "other"};//id对应123456
        if (goodsName != null && (!goodsName.equals(""))) {
            if ((goodsQuantitystr != null && !goodsQuantitystr.equals(""))) {
                if (goodsContent != null && (!goodsContent.equals(""))) {
                    if (part.getSize() != 0) {
                        int type = 0;
                        //获取商品类型id
                        for (int i = 0; i < typeId.length; i++) {
                            if (type_id.equals(typeId[i])) {
                                type = i + 1;
                            }
                        }
                        //商品价格
                        Float goodsQuantity = Float.parseFloat(request.getParameter("quantity-goods"));
                        User user = (User) request.getSession().getAttribute("loginUser");
                        Goods good = new Goods();
                        good.setType_id(type);
                        good.setNum(1);
                        good.setProducter_id(user.getId());
                        good.setName(goodsName);
                        good.setContent(goodsContent);
                        good.setPrice(goodsQuantity);
                        request.setAttribute("isCheck", true);
                        int maxid = 0;
                        FileOutputStream fos = null;
                        GoodsService goodsService = new GoodsServiceImpl();
                        maxid = goodsService.getMaxId();
                        good.setId(maxid + 1);
                        int id = good.getId();
                        byte[] bt = new byte[(int) part.getSize()];
                        String imagePathName = savePath + "/" + id + ".jpg";
                        good.setImage("static/goods_img/" + id + ".jpg");
                        fos = new FileOutputStream(imagePathName);
                        part.getInputStream().read(bt);
                        fos.write(bt);
                        Date date = new Date();
                        good.setCreatDate(date);
                        goodsService.doCreate(good);
                        response.sendRedirect("user/personal.jsp?tab=push&userid=" + user.getId() + "&success=1&info="
                                + java.net.URLEncoder.encode("添加成功", "UTF-8"));
                        return;
                    } else {
                        fileCheck = "需要上传物品图片";
                        request.setAttribute("fileCheck", fileCheck);
                        request.getRequestDispatcher("user/personal.jsp?tab=push&success=0").forward(request, response);
                    }
                } else {
                    contentCheck = "物品简介不能为空";
                    request.setAttribute("contentCheck", contentCheck);
                    request.getRequestDispatcher("user/personal.jsp?tab=push&success=0")
                            .forward(request, response);
                }

            } else {
                quantityCheck = "物品价格不能为空且要大于0";
            }
            request.setAttribute("quantityCheck", quantityCheck);
            request.getRequestDispatcher("user/personal.jsp?tab=push&success=0").forward(request, response);
        } else {
            nameCheck = "物品名称不能为空";
            request.setAttribute("nameCheck", nameCheck);
            request.getRequestDispatcher("user/personal.jsp?tab=push&success=0").forward(request, response);
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
